﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;

namespace OneSystems.Helpers
{
    public sealed class ImageHelper
    {
        private ImageHelper() { }
        public static byte[] ConvertImageToByteArray(Image imageToConvert)
        {

            byte[] Ret;

            try
            {

                using (MemoryStream ms = new MemoryStream())
                {
                    imageToConvert.Save(ms, ImageFormat.Png);
                    Ret = ms.ToArray();
                }
            }
            catch (Exception) { throw; }

            return Ret;
        }

        public static Image GetImage(byte[] imageArray)
        {
            Image newImage = null;

            if (imageArray.Length > 0)
            {
                using (MemoryStream ms = new MemoryStream(imageArray, 0, imageArray.Length))
                {

                    ms.Write(imageArray, 0, imageArray.Length);

                    newImage = Image.FromStream(ms, true);
                }
            }
            return newImage;
        }
    }
}
